/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package r2x;

import java.util.LinkedList;

/**
 *
 * @author przemek
 */
public class DTDFile {

private String content;
private Boolean closed = false;

DTDFile (){
    content = "<!DOCTYPE "
            + R2XApp.getApplication().getDbManager().getDbName()
            + " [\n\n";
}

public void updateContent(DTDElement root){
//printing first element
    content +=  "<!ELEMENT "
                + root.getName()
                + "(";
//printing children of first element as its attributes
    for (int i =0; i<root.getChildrenList().size(); i++){
        content +=  root.getChildrenList().get(i).getName()
                + root.getChildrenList().get(i).getCardinality()
                + ", ";
    }
    content = (String) content.subSequence(0, content.length()-2);
    content += ")>\n";

//printing children
    content += printChildren(root, "");
}

public String printChildren(DTDElement el, String spaces){
    String tempSpaces = spaces + "  ";
    String result = "";
    DTDElement temp;
    for (int i =0; i<el.getChildrenList().size(); i++){
        temp = el.getChildrenList().get(i);
        result += tempSpaces
                + "<!ELEMENT "
                + temp.getName()
                + "(";
        if (temp.getChildrenList().size() == 0)
            result += "#PCDATA";
        else {
            LinkedList<DTDElement> childList = temp.getChildrenList();
            for(int j=0; j<childList.size(); j++){
                result += childList.get(j).getName()
                        + childList.get(j).getCardinality()
                        + ", ";
            }
            result = (String) result.subSequence(0, result.length()-2);
        }
        
        result += ")>\n";
        result += printChildren(temp, tempSpaces);
    }
    return result;
}

public void closeFile(){
    content = content + "\n]>";
    closed = true;
}

public void append(String s){
    content = content + s;
}

@Override
public String toString(){
    return content;
}

    /**
     * @return the closed
     */
    public Boolean getClosed() {
        return closed;
    }

}
